package ${t.packagePath}.controller;

import java.util.List;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.ApiOperation;
import ${t.packagePath}.service.${t.mkNameBig}Service;
import ${t.packagePath}.model.${t.mkNameBig};
<#if cfg.saTokenAuthWay == 1 >import cn.dev33.satoken.stp.StpUtil;
<#else>import cn.dev33.satoken.annotation.SaCheckPermission;
</#if>


/**
 * Controller: ${t.tableName} -- ${t.tableComment}
 * @author ${cfg.author} 
 */
@RestController
@RequestMapping("/${t.varName}/")
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class ${t.mkNameBig}Controller {

	${t.mkNameBig}Service ${t.varName}Service;
	BeanSearcher searcher;
	@ApiOperation("增")
	@${cfg.apiMappingWayString}("add")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(${t.modelName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(${t.modelName} ${t.varNameSimple}){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>
		boolean b = ${t.varName}Service.save(${t.varNameSimple});
		return AjaxJson.getSuccessData(b);
	}

	@ApiOperation("删")
	@${cfg.apiMappingWayString}("delete")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(${t.modelName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson delete(${t.primaryKey.fieldType} id){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>
		boolean b = ${t.varName}Service.removeById(id);
		return AjaxJson.getByBoolean(b);
	}
	
	@ApiOperation("删-批量")
	@${cfg.apiMappingWayString}("deleteByIds")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(${t.modelName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson deleteByIds(String ids){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>
<#if t.hasFo("logic-delete")>
		int line = ${t.varName}Service.updateColumnByIds(${t.modelName}.TABLE_NAME, "${t.getDbColumnByFoType('logic-delete').columnName}", ${t.getDbColumnByFoType('logic-delete').tx.no}, ids);
<#else>
		String[] split = ids.split(",");
		boolean b = ${t.varName}Service.removeByIds(Arrays.asList(split));
</#if>
		return AjaxJson.getByBoolean(b);
	}

	@ApiOperation("改")
	@${cfg.apiMappingWayString}("update")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(${t.modelName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson update(${t.modelName} ${t.varNameSimple}){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>
		boolean b = ${t.varName}Service.updateById(${t.varNameSimple});
		return AjaxJson.getByBoolean(b);
	}

	@ApiOperation("查")
	@${cfg.apiMappingWayString}("getById")
	public AjaxJson getById(${t.primaryKey.fieldType} id){
		${t.modelName} ${t.varNameSimple} = ${t.varName}Service.getById(id);
		return AjaxJson.getSuccessData(${t.varNameSimple});
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@${cfg.apiMappingWayString}("getList")
	public AjaxJson getList(HttpServletRequest request) {
		SearchResult<${t.mkNameBig}> search = searcher.search(${t.mkNameBig}.class, MapUtils.flat(request.getParameterMap()));
    	return AjaxJson.getPageData(search.getTotalCount().longValue(), search.getDataList());
	}
}
